#include "widget.h"
#include "ui_widget.h"
#include <QPushButton>
#include <QIcon>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    //获取target的位置和尺寸信息
    QRect targetRect = ui->target->geometry();
    //设置按钮的图标
    ui->target->setIcon(QIcon(":/image/chicken.png"));
    ui->target->setIconSize(QSize(targetRect.x(),targetRect.y()));

    ui->pushButton_up->setIcon(QIcon(":/image/up.png"));
    ui->pushButton_up->setIconSize(QSize(50,50));

    ui->pushButton_down->setIcon(QIcon(":/image/down.png"));
    ui->pushButton_down->setIconSize(QSize(50,50));

    ui->pushButton_left->setIcon(QIcon(":/image/left.png"));
    ui->pushButton_left->setIconSize(QSize(50,50));

    ui->pushButton_right->setIcon(QIcon(":/image/right.png"));
    ui->pushButton_right->setIconSize(QSize(50,50));

    //给按钮设置快捷键
    //ui->pushButton_up->setShortcut(QKeySequence("ctrl + w"));//设置组合键
    //ui->pushButton_down->setShortcut(QKeySequence("s"));
    //ui->pushButton_left->setShortcut(QKeySequence("a"));
    //ui->pushButton_right->setShortcut(QKeySequence("d"));


    //还可以通过按键的枚举类型进行设置
    //ui->pushButton_up->setShortcut(QKeySequence(Qt::Key::Key_W));
    //ui->pushButton_up->setShortcut(QKeySequence(Qt::Key::Key_Up));
    ui->pushButton_up->setShortcut(QKeySequence(Qt::CTRL + Qt::Key::Key_W));
    ui->pushButton_down->setShortcut(QKeySequence(Qt::Key::Key_S));
    ui->pushButton_left->setShortcut(QKeySequence(Qt::Key::Key_A));
    ui->pushButton_right->setShortcut(QKeySequence(Qt::Key::Key_D));

    //开启鼠标点击的重复触发效果(键盘的重复触发效果默认是有的)
    ui->pushButton_up->setAutoRepeat(true);
    ui->pushButton_down->setAutoRepeat(true);
    ui->pushButton_left->setAutoRepeat(true);
    ui->pushButton_right->setAutoRepeat(true);

}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_up_clicked()
{
    QRect rect = ui->target->geometry();
    //设置按钮的移动后的位置
    ui->target->setGeometry(rect.x(),rect.y() - 5,rect.width(),rect.height());
}

void Widget::on_pushButton_down_clicked()
{
    QRect rect = ui->target->geometry();
    //设置按钮的移动后的位置
    ui->target->setGeometry(rect.x(),rect.y() + 5,rect.width(),rect.height());
}

void Widget::on_pushButton_left_clicked()
{
    QRect rect = ui->target->geometry();
    //设置按钮的移动后的位置
    ui->target->setGeometry(rect.x() - 5,rect.y(),rect.width(),rect.height());
}

void Widget::on_pushButton_right_clicked()
{
    QRect rect = ui->target->geometry();
    //设置按钮的移动后的位置
    ui->target->setGeometry(rect.x() + 5,rect.y(),rect.width(),rect.height());
}
